package cn.wolfcode.wolf2w.service.imp;

import cn.wolfcode.wolf2w.domain.UserInfo;
import cn.wolfcode.wolf2w.domain.View;
import cn.wolfcode.wolf2w.repository.UserInfoRepository;
import cn.wolfcode.wolf2w.repository.ViewRepository;
import cn.wolfcode.wolf2w.service.IViewService;
import org.apache.catalina.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class IViewServiceImpl implements IViewService {

    @Autowired
    private ViewRepository viewrepository;
    @Autowired
    private UserInfoRepository userInfoRepository;


    @Override
    public List<UserInfo> get(String uid) {
        //根据personId查询
        View view = viewrepository.findBypersonId(uid);
        List<UserInfo> users = new ArrayList<>();

        for (String user : view.getUserId()) {
            users.add(userInfoRepository.findById(user).get());
        }
        view.setUser(users);

        return users.subList(users.size() - 8, users.size());
    }

    @Override
    public void save(View view) {
        view.setId(null);
        viewrepository.save(view);
    }

    @Override
    public void update(View view) {
        viewrepository.save(view);
    }

    @Override
    public View getView(String sid) {
        return viewrepository.findById(sid).orElse(null);
    }


}
